package com.banshuibao.bsb_android.http;

import android.content.Context;
import android.text.TextUtils;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.util.IntentManager;
import com.banshuibao.bsb_android.util.LogUtil;
import com.banshuibao.bsb_android.util.XToast;
import com.google.gson.Gson;

/**
 * Created by zyh on 2017/2/7.
 */

public abstract class HttpResponse implements OnHttpResponseListener {

    private Context context;

    private Class z;

    public HttpResponse(Context context) {
        this.context = context;
    }

    public HttpResponse(Context context, Class z) {
        this.context = context;
        this.z = z;
    }

    @Override
    public void success(String json) {
        Gson gson = new Gson();
        if (z != null) {
            try {
                onResult(gson.fromJson(json, z));
            } catch (Exception e) {
                e.printStackTrace();
                LogUtil.e(e.getMessage());
                //onFailure(e.toString());
            }
        } else {
            onResult(json);
        }
    }

    @Override
    public void onError(String code, String msg) {
        LogUtil.e(msg);
        if (context != null)
            if (TextUtils.equals(code, Constants.Code.LOGIN_AGAIN)) {
                IntentManager.goLoginActivity(context);
                XToast.showToast(context, msg);
            }
//            else if (TextUtils.equals(code, Constants.Code.ERROR)) {
//
//            }
            else {
                if (!TextUtils.isEmpty(msg))
                    XToast.showToast(context, msg);
            }
    }

    @Override
    public void onFailure(String msg) {
        LogUtil.e(msg);
        if (context != null)
            XToast.showToast(context, context.getResources().getString(R.string.new_work_error));
    }

    @Override
    public void responseEnd() {

    }

}
